<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
<#assign classNameAllLower = className?lower_case>
<#assign hasLaydate = 'false'>
<#assign hasLayedit = 'false'>
<#assign hasImage = 'false'>
<#assign hasParentEnum = 'false'>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <#if adminSearchField?? && adminSearchField != "">
                        <#list adminSearchField?split(",") as field>
                            <#if field?contains("_") && field?contains(".")>
                                <#if parentTableList?? && (parentTableList?size>0)>
                                    <#list parentTableList as parentTable>
                                        <#if adminSearchField?? && adminSearchField != "">
                                            <#if field?contains(parentTable.sqlName)>
                                                <#list parentTable.columns as parentColumn>
                                                    <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                                        <#if parentColumn.isEnum>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">${parentTable.remarks}${parentColumn.columnAlias}：</label>
                        <div class="layui-input-inline mr0">
                            <select name="${parentTable.className?uncap_first}${parentColumn.columnName}">
                                <option value="">请选择${parentColumn.columnAlias}</option>
                                <#list parentColumn.enumList as enum>
                                <option value="${enum.alias}">${enum.desc}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                                                        <#else>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">${parentTable.remarks}${parentColumn.columnAlias}：</label>
                        <div class="layui-input-inline mr0">
                            <input name="${parentTable.className?uncap_first}${parentColumn.columnName}" class="layui-input" type="text" placeholder="请输入${parentColumn.columnAlias}"/>
                        </div>
                    </div>
                                                        </#if>
                                                        <#break>
                                                    </#if>
                                                </#list>
                                            </#if>
                                        </#if>
                                    </#list>
                                </#if>
                            <#else>
                                <#list table.columns as column>
                                    <#if column.sqlName == field>
                                        <#if column.isEnum>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">${column.columnAlias}：</label>
                        <div class="layui-input-inline mr0">
                            <select name="${column.columnNameLower}">
                                <option value="">请选择${column.columnAlias}</option>
                                <#list column.enumList as enum>
                                <option value="${enum.alias}">${enum.desc}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                                        <#elseif column.isDateTimeColumn>
                                            <#assign hasLaydate = 'true'>
                    <div class="layui-inline">
                        <label class="layui-form-label">${column.columnAlias}：</label>
                        <div class="layui-input-inline">
                            <input name="${column.columnNameLower}" class="layui-input date-icon" type="text" placeholder="选择${column.columnAlias}" autocomplete="off"/>
                        </div>
                        <input name="start${column.columnName}" type="hidden"/>
                        <input name="end${column.columnName}" type="hidden"/>
                    </div>
                                        <#else>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">${column.columnAlias}：</label>
                        <div class="layui-input-inline mr0">
                            <input name="${column.columnNameLower}" class="layui-input" type="text" placeholder="请输入${column.columnAlias}"/>
                        </div>
                    </div>
                                        </#if>
                                        <#break>
                                    </#if>
                                </#list>
                            </#if>
                        </#list>
                    </#if>
                    <div class="layui-inline">
                        <#if adminSearchField?? && adminSearchField != "">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                        </#if>
                        <#if adminHasAdd == 'true'>
                        <button id="${classNameLower}BtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        </#if>
                        <button id="${classNameLower}BtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="${classNameLower}Table" lay-filter="${classNameLower}Table"></table>
        </div>
    </div>
</div>
<#if adminHasAdd == 'true' || adminHasEdit == 'true' || adminHasDelete == 'true' || adminHasAudit == 'true'>

<!-- 表格操作列 -->
<script type="text/html" id="${classNameLower}TableBar">
    <#if adminHasEdit == 'true'>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    </#if>
    <#if adminHasDelete == 'true'>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </#if>
</script>
</#if>
<#if adminSearchField?? && adminSearchField != "">
    <#list adminSearchField?split(",") as field>
        <#if field?contains("_") && field?contains(".")>
            <#if parentTableList?? && (parentTableList?size>0)>
                <#list parentTableList as parentTable>
                    <#if adminSearchField?? && adminSearchField != "">
                        <#if field?contains(parentTable.sqlName)>
                            <#list parentTable.columns as parentColumn>
                                <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                    <#if parentColumn.isEnum>
                                        <#list parentColumn.enumList as enum>
<script type="text/html" id="#${parentTable.className?uncap_first}${parentColumn.columnName}Enum">
    <#list parentColumn.enumList as enum>
    <#if enum_index == 0>
    {{#  if(d.${parentColumn.columnNameLower} == '${enum.desc}'){}}
        <span>${enum.desc}</span>
    <#elseif enum_index == parentColumn.enumList?size - 1>
    {{#}else if(d.${parentColumn.columnNameLower} == '${enum.desc}'){}}
        <span>${enum.desc}</span>
    {{#}}}
    <#else>
    {{#}else if(d.${parentColumn.columnNameLower} == '${enum.desc}'){}}
        <span>${enum.desc}</span>
    </#if>
    </#list>
</script>
                                            <#break>
                                        </#list>
                                    </#if>
                                    <#break>
                                </#if>
                            </#list>
                        </#if>
                    </#if>
                </#list>
            </#if>
        </#if>
    </#list>
</#if>

<!-- 表单弹窗 -->
<script type="text/html" id="${classNameLower}Form">
    <form lay-filter="${classNameLower}Form" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <#list table.columns as column>
            <#if !adminFormRemoveField?? || adminFormRemoveField == "" || !adminFormRemoveField?contains(column.sqlName)>
                <#if column.pk || column.sqlName == 'version' || column.sqlName == 'create_time' || column.sqlName == 'update_time' || column.sqlName == 'is_deleted' || column.sqlName == 'remark'>
                <#elseif column.isDateTimeColumn>
                <#elseif column.sqlTypeName == "LONGTEXT" || column.sqlTypeName == "TEXT" || column.sqlTypeName == "MEDIUMTEXT">
                    <#assign hasLayedit = 'true'>
        <div class="layui-form-item">
            <label class="layui-form-label">${column.columnAlias}</label>
            <div class="layui-input-block">
                <textarea id="${column.columnNameLower}" name="${column.columnNameLower}" placeholder="请输入内容(200字以内)" class="layui-textarea" maxlength="200" <#if !column.nullable>lay-verify="required" required</#if>></textarea>
            </div>
        </div>
                <#elseif column.isEnum>
        <div class="layui-form-item">
            <label class="layui-form-label">${column.columnAlias}</label>
            <div class="layui-input-block">
                <select name="${column.columnNameLower}">
                    <#list column.enumList as enum>
                        <option value="${enum.alias}">${enum.desc}</option>
                    </#list>
                </select>
            </div>
        </div>
                <#elseif column.isImage>
                    <#assign hasImage = 'true'>
        <div class="layui-form-item">
            <label class="layui-form-label">${column.columnAlias?substring(0, column.columnAlias?index_of(":"))}</label>
            <div id="btnUpload${column_index}" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传文件</div>
            <div id="btnUpload${column_index}_path" style="margin-left: 260px;line-height: 40px;"></div>
            <input name="${column.columnNameLower}" type="hidden"/>
        </div>
                <#else>
        <div class="layui-form-item">
            <label class="layui-form-label">${column.columnAlias}</label>
            <div class="layui-input-block">
                <input name="${column.columnNameLower}" placeholder="请输入${column.columnAlias}" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
                </#if>
            </#if>
        </#list>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="${classNameLower}FormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table'<#if hasLaydate == 'true'>, 'laydate'</#if>, 'admin'<#if hasLayedit == 'true'>,
    'layedit'</#if><#if hasImage == 'true'>, 'upload'</#if>], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
            <#if hasLaydate == 'true'>
        var laydate = layui.laydate;
            </#if>
        var config = layui.config;
        var admin = layui.admin;
            <#if hasLayedit == 'true'>
        var layedit = layui.layedit;
            </#if>
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#${classNameLower}Table',
            url: config.base + '${productName}/${moduleName}/admin/${classNameLower}/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                        <#-- 有后台搜索字段 -->
                        <#if adminSearchField?? && adminSearchField != "">
                            <#list adminSearchField?split(",") as field>
                                <#if field?contains("_") && field?contains(".")>
                                    <#if ancestorTableList?? && (ancestorTableList?size>0)>
                                        <#list ancestorTableList as ancestorTable>
                                            <#if adminSearchField?? && adminSearchField != "">
                                                <#if field?contains(ancestorTable.sqlName)>
                                                    <#list ancestorTable.columns as ancestorColumn>
                                                        <#if ancestorColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                                            <#if ancestorColumn.isEnum>
                                                                <#list ancestorColumn.enumList as enum>
                ,{field: '${ancestorTable.className?uncap_first}${ancestorColumn.columnName}', title: '${ancestorTable.remarks}${ancestorColumn.columnAlias}'}
                                                                    <#break>
                                                                </#list>
                                                            <#else>
                ,{field: '${ancestorTable.className?uncap_first}${ancestorColumn.columnName}', title: '${ancestorTable.remarks}${ancestorColumn.columnAlias}'}
                                                            </#if>
                                                            <#break>
                                                        </#if>
                                                    </#list>
                                                </#if>
                                            </#if>
                                        </#list>
                                    </#if>
                                    <#if parentTableList?? && (parentTableList?size>0)>
                                        <#list parentTableList as parentTable>
                                            <#if adminSearchField?? && adminSearchField != "">
                                                <#if field?contains(parentTable.sqlName)>
                                                    <#list parentTable.columns as parentColumn>
                                                        <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                                            <#if parentColumn.isEnum>
                                                                <#list parentColumn.enumList as enum>
                ,{field: '${parentTable.className?uncap_first}${parentColumn.columnName}', title: '${parentTable.remarks}${parentColumn.columnAlias}'}
                                                                    <#break>
                                                                </#list>
                                                            <#else>
                ,{field: '${parentTable.className?uncap_first}${parentColumn.columnName}', title: '${parentTable.remarks}${parentColumn.columnAlias}'}
                                                            </#if>
                                                            <#break>
                                                        </#if>
                                                    </#list>
                                                </#if>
                                            </#if>
                                        </#list>
                                    </#if>
                                </#if>
                            </#list>
                        </#if>
                        <#-- 遍历列表 -->
                        <#list table.columns as column>
                        <#-- 有后台移除字段 -->
                            <#if adminTableRemoveField?? && adminTableRemoveField != "" && adminTableRemoveField?contains(column.sqlName)>
                            <#-- 移除基础字段 -->
                            <#elseif column.pk || column.sqlName == 'version' || column.sqlName == 'create_time' || column.sqlName == 'update_time' || column.sqlName == 'is_deleted' || column.sqlName == 'remark' || column.sqlName == 'creator' || column.sqlName == 'modifier'>
                            <#-- 移除Text字段 -->
                            <#elseif column.sqlTypeName == "LONGTEXT" || column.sqlTypeName == "TEXT" || column.sqlTypeName == "MEDIUMTEXT">
                            <#-- 处理枚举字段 -->
                            <#elseif column.isEnum>
                ,{field: '${column.columnNameLower}Desc', sort: true, title: '${column.columnAlias}'}
                            <#-- 处理图片字段 -->
                            <#elseif column.isImage>
                ,{field: '${column.columnNameLower}', sort: true, title: '${column.columnAlias?substring(0, column.columnAlias?index_of(":"))}', templet: function (d) {return '<img src="'+ d.${column.columnNameLower} +'" style="height: 20px;"/>';}}
                            <#else>
                ,{field: '${column.columnNameLower}', sort: true, title: '${column.columnAlias}'}
                            </#if>
                        </#list>
                        <#list table.columns as column>
                            <#-- 创建时间默认放最后 -->
                            <#if column.sqlName == 'create_time'>
                ,{field: 'createTime', sort: true, title: '${column.columnAlias}'}
                                <#break>
                            </#if>
                        </#list>
                        <#if adminHasAdd == 'true' || adminHasEdit == 'true' || adminHasDelete == 'true' || adminHasAudit == 'true'>
                ,{align: 'center', toolbar: '#${classNameLower}TableBar', title: '操作', width: 200}
                        </#if>
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                <#if adminHasEdit == 'true'>
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                </#if>
                <#if adminHasDelete == 'true'>
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
                </#if>
            }
        });

        // 工具条点击事件
        table.on('tool(${classNameLower}Table)', function (obj) {
            var data = obj.data;
            <#if adminHasEdit == 'true'>
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            }</#if><#if adminHasDelete == 'true'> else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }</#if>
        });

        //监听排序
        table.on('sort(${classNameLower}Table)', function(obj){
            table.reload('${classNameLower}Table', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

            <#if adminSearchField?? && adminSearchField != "">
                <#list adminSearchField?split(",") as field>
                    <#if field?contains("_") && field?contains(".")>
                        <#if parentTableList?? && (parentTableList?size>0)>
                            <#list parentTableList as parentTable>
                                <#if adminSearchField?? && adminSearchField != "">
                                    <#if field?contains(parentTable.sqlName)>
                                        <#list parentTable.columns as parentColumn>
                                            <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                                <#if parentColumn.isEnum && hasParentEnum = 'false'>
                                                    <#assign hasParentEnum = 'true'>
        //渲染下拉框
        form.render('select');

                                                </#if>
                                                <#break>
                                            </#if>
                                        </#list>
                                    </#if>
                                </#if>
                            </#list>
                        </#if>
                    <#else>
                        <#list table.columns as column>
                            <#if column.sqlName == field && column.isDateTimeColumn>
        //时间范围
        laydate.render({
            elem: 'input[name="${column.columnNameLower}"]',
            type: 'date',
            range: true,
            done: function(value, date, endDate){
                var dateValue = value.split(" - ");
                $("input[name='start${column.columnName}']").val(dateValue[0] + " 00:00:00");
                $("input[name='end${column.columnName}']").val(dateValue[1] + " 00:00:00");
            }
        });

                            <#elseif (column.sqlName == field && column.isEnum) || hasParentEnum == 'true'>
        //渲染下拉框
        form.render('select');

                            </#if>
                        </#list>
                    </#if>
                </#list>
        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

            </#if>
            <#if adminHasAdd == 'true'>
        // 添加按钮点击事件
        $('#${classNameLower}BtnAdd').click(function () {
            showEditModel();
        });

            </#if>
        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: <#if hasLayedit == 'true'>'${600 + 20*(table.columns?size - 1)}px'<#elseif hasImage == 'true'>'${500 + 20*(table.columns?size - 1)}px'<#else>'${400 + 20*(table.columns?size - 1)}px'</#if>,
                offset: '65px',
                title: data ? '修改${table.remarks}' : '添加${table.remarks}',
                content: $('#${classNameLower}Form').html(),
                success: function () {
                <#list table.columns as column>
                    <#if column.isImage>
                    upload.render({
                        elem: '#btnUpload${column_index}'
                        ,url: config.upload_url + 'upload?path='+img_path
                        ,data: {access_token : config.getToken()}
                        ,accept: 'file'
                        ,exts: 'jpg|png'
                        ,done: function(res){
                            if(res.code == 200){
                                layer.msg(res.msg, {icon: 1});
                                $('#btnUpload${column_index}_path').html('<img src="'+ res.data.url +'" style="height: 35px;" />');
                                $('input[name="${column.columnNameLower}"]').val(res.data.url);
                            }else if(res.code == 502){
                                return location.replace('admin/login.html');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                        <#break>
                    </#if>
                </#list>
                <#list table.columns as column>
                    <#if column.sqlTypeName == "LONGTEXT" || column.sqlTypeName == "TEXT" || column.sqlTypeName == "MEDIUMTEXT">
                    var layeditIndex${column_index} = layedit.build('${column.columnNameLower}',{
                        uploadImage: {
                            url: config.upload_url + 'layuiUpload?access_token=' +config.getToken(),
                            type: 'post'
                        }
                    });

                    </#if>
                </#list>
                <#list table.columns as column>
                    <#if column.isImage>
                    if (data){
                        $('#btnUpload${column_index}_path').html('<img src="'+ data.${column.columnNameLower} +'" style="height: 35px;" />');
                    }

                    <#elseif column.isEnum>
                        <#list column.enumList as enum>
                    if (data){
                        var option = $("form select[name='${column.columnNameLower}']").find("option");
                        layui.each(option, function(index, item){
                            if(data.${column.columnNameLower}  == item.text){
                                data.${column.columnNameLower} = item.value;
                            }
                        });
                    }

                            <#break>
                        </#list>
                    </#if>
                </#list>
                    form.val('${classNameLower}Form', data);
                    // 表单提交事件
                    form.on('submit(${classNameLower}FormSubmit)', function (d) {
                        layer.load(2);
                        <#list table.columns as column>
                            <#if column.sqlTypeName == "LONGTEXT" || column.sqlTypeName == "TEXT" || column.sqlTypeName == "MEDIUMTEXT">
                        d.field.${column.columnNameLower} = layedit.getContent(layeditIndex${column_index});
                            </#if>
                        </#list>
                        admin.req(data ? '${productName}/${moduleName}/admin/${classNameLower}/update' : '${productName}/${moduleName}/admin/${classNameLower}/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('${classNameLower}Table');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        <#if adminHasDelete == 'true'>
        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('${productName}/${moduleName}/admin/${classNameLower}/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        </#if>
        // 导出excel
        $('#${classNameLower}BtnExport').click(function () {
            var checkRows = table.checkStatus('${classNameLower}Table');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>